package com.huika.renrench.control.my.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.adapter.AreaInfoAdapter;
import com.huika.renrench.db.areainfo.AreaInfoDbHelper;
import com.huika.renrench.support.bean.AreaInfo;
import com.huika.renrench.view.TitleBarHelper;



/**
 * 选择省份的页面
 * 
 * @description：
 * @author ldm
 * @date 2014年10月11日 下午4:28:13
 */
//public class ChoiceProvienceActivity extends ActivityObjectSupport implements OnClickListener {
public class ChoiceProvienceActivity extends BaseAct implements OnClickListener {
	private ListView listView;
	private AreaInfoAdapter mAddressAdapter;
	private List<AreaInfo> provinceList = new ArrayList<AreaInfo>();
	private ImageButton mBackBtn;
	private AreaInfoDbHelper mAreaInfoDbHelper; 
	private int mLevel = 1;
	private AreaInfo mProvienceInfo, mCityInfo, mAreaInfo;

	int mBeginLevel = 3;
	int mAreaID_shenzhen = 440300;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mAreaInfoDbHelper = new AreaInfoDbHelper();
		setContentView(R.layout.layout_choice_area);
		initViews();
		mLevel = mBeginLevel;
		initDatas();
	}
	
	protected void initViews() {
		new TitleBarHelper(this, -1, R.string.choice_area_title);
		
		listView = (ListView) findViewById(R.id.eshop_select_lv);
		mAddressAdapter = new AreaInfoAdapter(ChoiceProvienceActivity.this, provinceList, false);
		listView.setAdapter(mAddressAdapter);
		listView.setOnItemClickListener(onItemClickListner);
	}

	protected boolean initDatas() {
		switch (mLevel) {
			case 1:
				provinceList = mAreaInfoDbHelper.getProvinceList();
				break;
			case 2:
				provinceList = mAreaInfoDbHelper.getAreaListByParentID(mProvienceInfo.getAreaId());
				break;
			case 3:
				
				provinceList = mAreaInfoDbHelper.getAreaListByParentID(mAreaID_shenzhen); //mCityInfo.getAreaId());
				break;
		}
		
		if(mLevel>3 || provinceList==null || provinceList.size()==0){
			return false;
		}
		
		mAddressAdapter.setData(provinceList);
		return true;
	}
	
	@Override
    public void onBackPressed() {
		if (mLevel > mBeginLevel) {
			mLevel--;
			initDatas();
		} else {
			super.onBackPressed();
		}
    }


	private OnItemClickListener onItemClickListner = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if (parent instanceof ListView) {
				int realPosition = position - ((ListView) parent).getHeaderViewsCount();
				if (realPosition >= 0 && realPosition < mAddressAdapter.getCount()) {
					if (mLevel == 1) {
						mProvienceInfo = provinceList.get(realPosition);
					} else if(mLevel == 2){
						mCityInfo = provinceList.get(realPosition);
					}
					mLevel++;
					if(!initDatas()){//如果没有下一级，则终止
						String address = "";
//						String address = mProvienceInfo.getAreaName() + " " + mCityInfo.getAreaName();
						int areaId = -1; //mCityInfo.getAreaId();
						if(provinceList!=null && provinceList.size()>0){
							mAreaInfo = provinceList.get(realPosition);
//							address = address + " " + mAreaInfo.getAreaName();
							address = mAreaInfo.getNamePath().replace("^", " ").trim();
							areaId = mAreaInfo.getAreaId();
						}
						
						// 地区选择完毕， 返回调用界面
						Bundle bundle = new Bundle();
						bundle.putString("CHOICE_AREA_RESULT", address); // 中文地址
						bundle.putInt("CHOICE_AREA_RESULT_ID", areaId); // 地区ID
						Intent mIntent = getIntent();
						mIntent.putExtras(bundle);
						setResult(Activity.RESULT_OK, mIntent);
						finish();
					}
				}
			}
		}
	};

	public void jumpToActivity(Context from, Class to, Bundle mBundle, Boolean isFinish) {
		Intent intent = new Intent(from, to);
		intent.putExtras(mBundle);
		startActivity(intent);
//		startAnimate(from);
		if (isFinish) {
			finish();
		}
	}

	@Override
	public void onClick(View v) {
//		if (v.getId() == R.id.btn_head_left) {
//			jumpToActivity(EshopPublishMessageActivity.class, true);
//		}
	}

	@Override
	public void setRootView() {
		
	}

}